package com.company;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int[] array = new int []{5,4,2,7,8,1,9,6,3,0};
        int[] array2 = new int []{5,2,3,1};

        //bubbleSort(array);
        //selectSort(array);
        //insertSort(array);
        //shellSort2(array);
        //heapSort(array);
        //mergeSort(array);
        selectSort2(array2);
        System.out.println(Arrays.toString(array2));
        System.out.println(array.length);
    }
        // 冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            boolean flag = false;
            for (int j = array.length - 1; j > i; j--) {
                if(array[j] < array[j - 1]){
                    int tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                    flag = true;
                }
            }
            if(!flag){
                break;
            }
        }
    }

    public static void selectSort2(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            int minIndex = left;
            int maxIndex = left;

            for(int j = left + 1;j <= right; j++){
                if(nums[j] < nums[minIndex]){
                    minIndex = j;
                }
                if(nums[j] > nums[maxIndex]){
                    maxIndex = j;
                }
            }
            swap(nums,minIndex,left);
            if(maxIndex == left) maxIndex = minIndex;
            swap(nums,maxIndex,right);
            left++;
            right--;
        }
    }
/*    private static void selectSort2(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            int minIndex = i;
            int maxIndex = nums.length-1-i;
            for (int j = i ; j < nums.length - i; j++) {
                if (nums[j] < nums[minIndex]) {
                    minIndex = j;
                }
                if(nums[j] > nums[maxIndex]){
                    maxIndex = j;
                }
            }
            swap(nums, i, minIndex);
            swap(nums,nums.length-1-i,maxIndex);
        }
    }*/

    // 选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if(array[j] < array[minIndex]){
                    minIndex = j;
                }
            }
            int tmp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = tmp;
        }
    }
    // 直接插入排序
    /*public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }*/
    public static void insertSort(int[] array){
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

        // 希尔排序
    public static void shellSort(int[] array){
            int gap = array.length;
            while(gap > 1){
                gap /= 2;
                shell(array,gap);
            }
    }
    public static void shellSort2(int[] array){
        int gap = array.length;
        while(gap >= 1){
            shell2(array,gap);
            gap /= 2;
        }
    }

    private static void shell2(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int j = i - gap;
            int tmp = array[i];
            for (;j >= 0; j -= gap){
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if(array[j] > tmp){
                    array[j+gap] = array[j];
                }else{
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    // 堆排序
    public static void heapSort(int[] array) {
        createHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }
    private static void createHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0; parent--) {
            siftDown(array,parent,array.length);
        }

    }
    private static void siftDown(int[] array, int parent, int length) {
        int child = 2 * parent + 1;
        while (child < length) {
            if(child + 1 < length && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array, parent, child);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }
    private static void swap(int[] array, int x, int y) {
        int tmp = array[x];
        array[x] = array[y];
        array[y] = tmp;
    }
        //归并排序
        public static void mergeSort(int[] array) {
            mergeSortTmp(array,0,array.length-1);
        }
        private static void mergeSortTmp(int[] array,int left,int right) {
            if(left >= right) {
                return;
            }
            int mid = (left + right) / 2;
            mergeSortTmp(array,left,mid);
            mergeSortTmp(array,mid+1,right);
            //走到这里 全部分解完毕
            // 合并
            merge(array,left,mid,right);
        }
        private static void merge(int[] array, int left, int mid, int right) {
            int[] tmp = new int[right-left+1];
            int k = 0;
            int s1 = left;
            //int e1 = mid;
            int s2 = mid+1;
            //int e2 = right;
            while (s1 <= mid && s2 <= right) {
                if(array[s1] <= array[s2]) {
                    tmp[k++] = array[s1++];
                }else {
                    tmp[k++] = array[s2++];
                }
            }
            while (s1 <= mid) {
                tmp[k++] = array[s1++];
            }
            while (s2 <= right) {
                tmp[k++] = array[s2++];
            }
            //可以保证tmp数组 是有序的
            for (int i = 0; i < k; i++) {
                array[i+left] = tmp[i];
            }
        }
        //快速排序
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int partition2(int[] array, int left, int right) {
        int prev = left ;
        int cur = left+1;
        while (cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,cur,prev);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }

    private static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }



}

